package com.tsx.leetcode.hot100.easy;

import com.tsx.leetcode.hot100.common.TreeNode;

/**
 * <a href="https://leetcode.cn/problems/symmetric-tree/description">对称二叉树</a>
 */
public class Q101IsSymmetric {
    public static void main(String[] args) {
        // 测试数据
        TreeNode l1 = new TreeNode(1);
        TreeNode l2 = new TreeNode(2);
        TreeNode l3 = new TreeNode(2);
        TreeNode l4 = new TreeNode(3);
        TreeNode l5 = new TreeNode(4);
        TreeNode l6 = new TreeNode(4);
        TreeNode l7 = new TreeNode(3);
        l1.left = l2;
        l1.right = l3;
        l2.left = l4;
        l2.right = l5;
        l3.left = l6;
        l3.right = l7;
        System.out.println(new Q101IsSymmetric().isSymmetric(l1));
    }

    public boolean isSymmetric(TreeNode root) {
        return root == null || check(root.left, root.right);
    }

    private boolean check(TreeNode left, TreeNode right) {
        if (left == null && right == null) return true;
        if (left == null || right == null || left.val != right.val) return false;
        return check(left.left, right.right) && check(left.right, right.left);
    }
}
